package admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.common.EmiJsonObj;
import com.common.FileUploadUtils;
import com.common.ResultData;
import com.filter.BaseController;

@Controller
@RequestMapping("/upload")
public class UploadFileController extends BaseController{

	@RequestMapping("/imagestr")
	@ResponseBody
	public void UploadFileString(HttpServletRequest request,HttpServletResponse response,String imgData,String type,String Orientation) throws IOException {
		ResultData<String> rd = new ResultData<>();
		String url = FileUploadUtils.GenerateImage(request, imgData.substring(23), type);
		rd.setType(1);
		rd.setMessage(url);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(EmiJsonObj.fromObject(rd).toString());
	}
	
	@RequestMapping("/image")
	@ResponseBody
	public void image(@RequestParam("file")MultipartFile file,String path,HttpServletResponse response) throws IOException { 
		ResultData<Object> rd = new ResultData<>();
		try {
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
			if("jpg,png,bmp,jpeg".indexOf(suffix.toLowerCase())<0){
				rd.setSuccess(false);
				rd.setMessage("文件格式不正确，请上传jpg,png,bmp,jpeg格式的图片文件！");
			}else{ // 10m
				rd.setMessage(FileUploadUtils.upload(file, path, getReq()));
				rd.setSuccess(true);
			}

		} catch (Exception e) {
			rd.setSuccess(false);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(EmiJsonObj.fromObject(rd).toString());
	}
	
	@RequestMapping("/uploadfile")
	@ResponseBody  
	public void springUpload(HttpServletRequest request) throws Exception{
		CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());

		ResultData<Map> rd = new ResultData<>();
		Map m = new HashMap();
		if(multipartResolver.isMultipart(request)){
			MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
			Iterator iter=multiRequest.getFileNames();
			String path = request.getParameter("path");
			String type = request.getParameter("type");
			while(iter.hasNext()){
				MultipartFile file=multiRequest.getFile(iter.next().toString());
				if(file!=null){
					String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
					if(type.equals("img")&&"jpg,png,bmp,jpeg".indexOf(suffix)<0){
						rd.setSuccess(false);
						rd.setMessage("文件格式不正确，请上传jpg,png,bmp,jpeg格式的文件！");
					}
					else if(type.equals("mp3")&&"mp3".indexOf(suffix)<0) {
						rd.setSuccess(false);
						rd.setMessage("文件格式不正确，请上传mp3格式的文件！");
					}
					else{
						rd.setSuccess(true);
						m.put("path", FileUploadUtils.upload(file, path, request));
						m.put("name", file.getOriginalFilename());
					}
				}
			}
		}
		rd.setRows(m);
		responseJson(rd);
	}
	
	@RequestMapping("/delfile")  
	@ResponseBody  
	public void delfile(String path,HttpServletResponse response) throws IOException { 
		ResultData<Object> rd = new ResultData<>();
		try {
			String realPath = getReq().getSession().getServletContext().getRealPath("/");
			rd.setSuccess(FileUploadUtils.delFile( realPath+path));
		} catch (Exception e) {
			rd.setSuccess(false);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(EmiJsonObj.fromObject(rd).toString());
	}
}
